/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.component.standard;

import java.sql.SQLException;
import java.util.List;
import org.just.dusin.ooii.component.Pipe;
import org.apache.log4j.Logger;
import org.dusin.webcoal.exception.OOIIException;
import org.just.dusin.ooii.component.kj.DataPackage;
import org.just.dusin.ooii.database.DatabaseConnectionSQL;
import org.just.dusin.ooii.database.MineData;
import org.just.dusin.ooii.database.OrgDao4SQLServer;

/**
 *
 * @author Administrator
 */
public class FilterPipe extends Pipe {
    List<String> allowedOrgCodeList;
    private static Logger logger = Logger.getLogger(FilterPipe.class.getName());
    private OrgDao4SQLServer orgDao;
    private DatabaseConnectionSQL dbc;

    public List<String> getAllowedOrgCodeList() {
        return allowedOrgCodeList;
    }   
    public void setAllowedOrgCodeList(List<String> allowedOrgCodeList) {
        this.allowedOrgCodeList = allowedOrgCodeList;
    }

    public DatabaseConnectionSQL getDbc() {
        return dbc;
    }

    public void setDbc(DatabaseConnectionSQL dbc) {
        this.dbc = dbc;
    }
    
    
    @Override
    public void process(String msg) throws OOIIException {
        
         try {            
            orgDao = new OrgDao4SQLServer(dbc.getConnection());
        } catch (SQLException ex) {
            dbc.close();
            logger.error(ex.getMessage());
            throw new OOIIException(ex);
        }
      
        DataPackage.setOrgDao(orgDao);
        List<MineData> mineDatas = DataPackage.dePack(msg);  
        if(msg==null||mineDatas.size()==0) return;
        
        MineData mineData = mineDatas.get(0);   //多个消息，假定是属于同一个煤矿的，所以只判断第一个消息所属煤矿
        String orgcode = mineData.getMineCode();
        if(allowedOrgCodeList.contains(orgcode)){
            sink.process(msg);
        }else{
            logger.info("丢弃无用消息："+msg);
        }                 
    }

    @Override
    public void process(List<String> msgs) throws OOIIException {        
        for(String msg:msgs){
            process(msg);
        }
    }
}
